package com.dmcb.trade.business.crawlers.article;

import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 圈圈网文章解析
 * Created by Administrator on 2016/6/28.
 */
@Service
public class QuanQuanWangParser extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.QUANQUANWANG_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "h2#subject";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "div.vt-info>div.fl";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div.topic-layer-photos";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.topic-layer-photos>p img[src]";
    }

    /**
     * 获取作者选择器
     *
     * @return 作者选择器
     */
    @Override
    protected String authorNameSelect() {
        return "div.vt-info>div.f1>a";
    }


    /**
     * 设置发布时间
     *
     * @param doc     网页
     * @param article 文章
     */
    @Override
    protected void setPostDate(Document doc, Article article) {
        Element element = doc.select(dateSelect()).first();
        Calendar cal = Calendar.getInstance();
        if (element != null) {
            int num;
            String time = text(element).replace(" ","");
            time = time.split("小学生")[1].substring(1);
            time = time.trim();
            if (time.contains("分")) { //如果有分钟，则获取多少分钟，
                num = Integer.parseInt(time.substring(0, time.indexOf("分")));
                cal.add(Calendar.MINUTE, -num);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                time = formatter.format(cal.getTime());
            } else if (time.contains("小时")) {//如果有小时，则获取多少小时，
                time = time.replace(" ", ""); //去除时间的空格
                num = Integer.parseInt(time.substring(0, time.indexOf("小时")));
                cal.add(Calendar.HOUR_OF_DAY, -num);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                time = formatter.format(cal.getTime());
            } else if (time.contains("天")) {//如果有天，则获取多少天，
                num = Integer.parseInt(time.substring(0, time.indexOf("天")));
                cal.add(Calendar.DAY_OF_MONTH, -num);//得到前5
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                time = formatter.format(cal.getTime());
            }
            Date date = DateUtil.parseDate(time);
            if (date != null) {
                article.setPostTime(date);
            }
        }
    }
    @Override
    protected String platform() {
        return "圈圈网";
    }

}
